﻿using ModernChannel.Model.Interface;
using ModernChannel.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Util;

namespace ModernChannel.Provider
{
    public class BilltoInfProvider : GenericProvider, IBilltoInfProvider
    {
        public List<BilltoInf> SearchBilltoInf(out int totalRecords, bool? isLowPriceGroup = null, string textsearch = null)
        {
            var query = repository.GetQuery<BilltoInf>();
            if (isLowPriceGroup == true)
            {
                query = query.Where(x => !string.IsNullOrEmpty(x.LowPriceGroup));
            }

            if (!textsearch.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.BilltoCode.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.FullName.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.CustomerCode.ToUpper().Contains(textsearch.ToUpper()) ||
                                         x.Address.ToUpper().Contains(textsearch.ToUpper()));
            }
            totalRecords = query.Count();
            return query.ToList();
        }

        public BilltoInf RetrieveBilltoInf(string billtocode)
        {
            if (billtocode.IsNullOrWhiteSpace()) return null;
            return repository.FindOne<BilltoInf>(x => x.BilltoCode.ToUpper() == billtocode.ToUpper());
        }
    }
}
